ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I have a file (let's call it a.gif). And I have a text file with a list of names in it.
What I need is some kind of script that will make a copies of a.gif; each new name of a.gif will correspond to 1 line from that text file.
So I get for instance 5000 copies of a.gif, called house.gif, car.gif, tux.gif and so on.
How can I realise this? I think a simple BASH script can do this, but I do not have a clue how
#!/bin/bash
# This script copies SRCFILE (see below) to all
# file names listed in LISTFILE.
#
# When a directory in LISTFILE does not exist yet,
# it will be created.
SRCFILE="a.gif"
LISTFILE="yourlist.txt"
while read NEWFILE ; do
mkdir -p "${NEWFILE%/*}"
cp -v $SRCFILE $NEWFILE
done <"$LISTFILE"
exit 0
What I do need it for? For a website that I am devoping. I'm, working with a huge set of items that has to be accomponied with the same banner everytime. When I do this the banner has the same name as the variable for the showed item so I only need 1 variable. In that way, if we want to, we can later change individual banners without having to rewrite the entire code; for now we'll just use one banner... that's why
I agree but am confused as why are you making all of those directories?
The way it looks to me is the name file has a bunch of names ....
list.txt
Code:
car
house
train
tux
fish
bear
truck
and your script would be like this ...
Code:
#!/bin/bash
# This script copies SRCFILE (see below) to all
# file names listed in LISTFILE.
SRCFILE="a.gif"
LISTFILE="list.txt"
while read NEWFILE ; do
cp -v $SRCFILE ${NEWFILE}.gif
done <"$LISTFILE"
exit 0
Originally posted by homey
I agree but am confused as why are you making all of those directories?
The way it looks to me is the name file has a bunch of names ....
It's just a feature,
With your list.txt my script wil have the same result.
I was more or less expecting some follow-up question like: "how to copy the files to other directories?", and possibly next: "how to copy them to a directory that doesn't exist yet?".
Hko, since you seem to be one who likes to take care of the widest range of scenarios, I thought I would point something to you:
Code:
$ ls -l
total 0
-rw-r--r-- 1 supmis supmis 0 Dec 24 14:35 asdf asdf
-rw-r--r-- 1 supmis supmis 0 Dec 24 14:35 a
-rw-r--r-- 1 supmis supmis 0 Dec 24 14:35 asdf asasd23
-rw-r--r-- 1 supmis supmis 0 Dec 24 14:35 b
$ ls -1 > /tmp/a.txt
$ cat /tmp/a.txt
asdf asdf
a
asdf asasd23
b
$ while read fname ; do
> echo "${fname}"
> done < /tmp/a.txt
asdf asdf
a
asdf asasd23
b
$ for fname in "$(cat /tmp/a.txt)" ; do
> echo "${fname}"
> done
asdf asdf
a
asdf asasd23
b
$
The point of this code is the leading space for filename " asdf asdf". In this case, for seems to be a better choice. This happends on those extremely rare occassions when the filename has leading space(s).
Originally posted by dustu76
Hko, since you seem to be one who likes to take care of the widest range of scenarios,
Heh..well yes, I guess so...
Quote:
I thought I would point something to you:
[..snip code..]
The point of this code is the leading space for filename " asdf asdf". In this case, for seems to be a better choice. This happends on those extremely rare occassions when the filename has leading space(s).
Good point! Though, as you say, it's a rare occasion. A bug that counts anyways. Thanks.
I wonder if it could be done right without the "for F in $(cat file.txt) ; do" construction. Generally I don't like this very much, because it can yield in a huge command line being executed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.